package net.maku.custom.biza.controller.customers;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import net.maku.custom.biza.controller.customers.query.CustomerInfoQuery;
import net.maku.custom.biza.controller.customers.query.CustomerSelectResponseVO;
import net.maku.custom.biza.controller.customers.query.CustomerShipmentsQuery;
import net.maku.custom.biza.controller.customers.vo.CustomerHResponseVO;
import net.maku.custom.biza.controller.customers.vo.CustomerInfoVO;
import net.maku.custom.biza.service.customs.CustomerInfoService;
import net.maku.framework.common.page.PageResult;
import net.maku.framework.common.utils.Result;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;

/**
* 客户信息
*
* @author NULL
* @since 1.0.0 2022-07-11
*/
@RestController
@RequestMapping("/customerInfo")
@Tag(name="客户信息")
@AllArgsConstructor
public class CustomerInfoController {
    private final CustomerInfoService customerInfoService;

    @GetMapping("/page")
    @Operation(summary = "分页")
    public Result<PageResult<CustomerInfoVO>> page(@Valid CustomerInfoQuery query){
        PageResult<CustomerInfoVO> page = customerInfoService.page(query);

        return Result.ok(page);
    }

    @GetMapping("/selectCustomerList")
    @Operation(summary = "分页")
    public Result<PageResult<CustomerInfoVO>> selectCustomerList(@Valid CustomerInfoQuery query){
        PageResult<CustomerInfoVO> page = customerInfoService.selectCustomerList(query);

        return Result.ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    public Result<CustomerInfoVO> get(@PathVariable("id") Long id){
        CustomerInfoVO convert = customerInfoService.selectRelById(id);
        return Result.ok(convert);
    }

    @PostMapping("/save")
    @Operation(summary = "保存")
    public Result save(@RequestBody CustomerInfoVO vo){
        return customerInfoService.save(vo);
    }

    @PostMapping("/update")
    @Operation(summary = "修改")
    public Result<String> update(@Valid @RequestBody CustomerInfoVO vo){
        customerInfoService.update(vo);

        return Result.ok();
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除")
    public Result<String> delete(List<Long> idList){
        customerInfoService.delete(idList);

        return Result.ok();
    }


    @PostMapping("/downloadShipments")
    @Operation(summary = "下载出货量")
    public void downloadShipments(HttpServletResponse response, @RequestBody CustomerShipmentsQuery query) {
        customerInfoService.downloadShipments(response,query);
    }


    @GetMapping("/getData")
    @Operation(summary = "客户下拉框")
    @PreAuthorize("hasAuthority('custom:customer:select')")
    public Result<CustomerHResponseVO> getData(@Valid Long id){
        return customerInfoService.getData(id);
    };



    @GetMapping("/getSelectData")
    @Operation(summary = "客户下拉框")
    @PreAuthorize("hasAuthority('custom:customer:select')")
    public Result<List<CustomerSelectResponseVO>> getSelectData(){
        return customerInfoService.getSelectData();
    };
}